// components/restaurantForm/restaurantForm.js
import prompt from '../../utils/prompt.js';
import Api from '../../utils/api.js';
import utils from '../../utils/util.js';
import {
  checkRequire,
  checkNumber,
  checkNumberRange,
  checkEarlier,
  checkLater
} from '../../utils/validator.js';

const app = getApp();

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    dateTimeArray: [],
    dateTime: [],

    dinnerDateEnd: utils.formatTime(new Date(Date.now() + 10 * 24 * 60 * 60 * 1000), 'YYYY-MM-DD'), // 10天后
    dinnerDateStart: utils.formatTime(new Date(), 'YYYY-MM-DD'),

    receptionTargetList: ['公司领导', '分公司领导', '政府领导'], // 接待对象列表
    mealTypeList: ['中餐', '火锅', '烧烤'], // 用餐类型列表
    beverageList: ['白酒', '啤酒', '红酒', '无'], // 酒水配置列表
    intentionMerchantList: [], // 意向商家列表

    others: '', // 其他留言
    userName: '',
    contacts: '', // 联系人手机号
    intentionMerchant: '', // 意向商家
    beverage: '', // 酒水配置
    mealType: '', // 用餐类型
    receptionTarget: '', // 接待对象
    dinnerDateTime: '', // 用餐时间
    dinerCount: '', // 用餐人数

    ps_intentionMerchant: true,
    ps_beverage: true,
    ps_mealType: true,
    ps_receptionTarget: true,
    ps_dinnerDateTime: true,
    ps_dinerCount: true
  },

  // 生命周期
  attached () {
    this.getIntentionBusiness();
    this.setData({
      userName: app.globalData.userInfo.userName,
      contacts: app.globalData.userInfo.userTel
    });
  },

  /**
   * 组件的方法列表
   */
  methods: {

    /* --===================数据接口===================-- */
    // 查询意向商家
    async getIntentionBusiness () {
      const res = await Api.intentionBusiness({
        sessionKey: app.globalData.sessionKey
      });
      if (res) {
        this.setData({ intentionMerchantList: res.message || [] });
      }
    },
    // 提交用餐请求
    async submitRestaurant () {
      const param = {
        'drinks': this.data.beverage,
        'intentionHotel': this.data.intentionMerchant,
        'linkNum': this.data.contacts,
        'mealNum': +this.data.dinerCount,
        'mealTime': this.data.dinnerDateTime,
        'mealType': this.data.mealType,
        'reception': this.data.receptionTarget,
        'remark': this.data.others
      };
      const res = await Api.applyMeal(param, app.globalData.sessionKey);
      if (res) {
        prompt.showToast('申请用餐成功', 'success', { mask: true });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    },

    /* --===================功能函数===================-- */
    checkInput () {
      let ps_dinerCount = false;
      let ps_dinnerDateTime = false;
      let ps_receptionTarget = false;
      let ps_mealType = false;
      let ps_beverage = false;
      let ps_intentionMerchant = false;

      ps_dinerCount = (
        checkRequire(this.data.dinerCount, '请输入用餐人数') &&
        checkNumber(this.data.dinerCount, '请输入正确的用餐人数') &&
        checkNumberRange(this.data.dinerCount, 1, 255, '用餐人数应在1人以上255人以下')
      );
      this.setData({ ps_dinerCount });

      if (ps_dinerCount) {
        const current = new Date();
        const tenDays = 10 * 24 * 60 * 60 * 1000;
        ps_dinnerDateTime = (
          checkRequire(this.data.dinnerDateTime, '请选择用餐时间') &&
          checkEarlier(new Date(this.data.dinnerDateTime), new Date(current.getTime() + tenDays), '请选择10日以内的时间') &&
          checkLater(new Date(this.data.dinnerDateTime), current, '无效的用餐时间')
        );
        this.setData({ ps_dinnerDateTime });
      }

      if (ps_dinnerDateTime) {
        ps_receptionTarget = checkRequire(this.data.receptionTarget, '请选择或输入你的接待对象');
        this.setData({ ps_receptionTarget });
      }

      if (ps_receptionTarget) {
        ps_mealType = checkRequire(this.data.mealType, '请选择或输入用餐类型');
        this.setData({ ps_mealType });
      }

      if (ps_mealType) {
        ps_beverage = checkRequire(this.data.beverage, '请选择或输入酒水配置');
        this.setData({ ps_beverage });
      }

      if (ps_beverage) {
        ps_intentionMerchant = checkRequire(this.data.intentionMerchant, '请选择或输入你的意向商家');
        this.setData({ ps_intentionMerchant });
      }

      return (
        ps_dinerCount &&
        ps_dinnerDateTime &&
        ps_receptionTarget &&
        ps_mealType &&
        ps_beverage &&
        ps_intentionMerchant
      );
    },

    /* --===================事件处理===================-- */
    // 用餐人数输入事件
    handleInput (e) {
      const param = {};
      param[e.currentTarget.dataset.state] = e.detail.value;
      this.setData(param);
    },
    // 获取 picker input 组件值
    handlePickerInputValue (e) {
      const param = {};
      param[e.currentTarget.dataset.value] = e.detail.value;
      this.setData(param);
    },
    // 表单提交
    handleSubmit () {
      if (this.checkInput()) {
        this.submitRestaurant();
      }
    }
  }
});
